package service

import (
	"app-box/api_error"
	"app-box/config"
	"app-box/models"
	"app-box/tools"
	filestore "app-box/tools/FileStore"
	"errors"
	"mime/multipart"
	"path/filepath"
)

type FileService struct {
}

func (s FileService) UploadFile(id models.IDType, key, md5 string, overwrite bool, File multipart.FileHeader) filestore.FileString {
	ID, err := id.ToString()
	if err != nil {
		panic(err)
	}
	// 校验MD5
	tools.PanicFileMD5Check(md5, File)
	// 创建临时Key
	tmpKey := filestore.CreateTmpKey(ID, filestore.FileKeyTypeLocal, key)
	savePath := filepath.Join(config.LocalFile.FilaSavePath, tmpKey.ObjectKey)
	if tools.FileExist(savePath) { // 临时文件存在冲突
		panic(api_error.IOError(errors.New("File Allready Exist")))
	}
	tools.SaveFileTo(File, savePath)
	return tmpKey.ToFileStr()
}
